package main

import (
	"fmt"

	"github.com/xwb1989/sqlparser"
	"yu.DbPrime/src/db"

	"yu.DbPrime/src/distributed"
)

func main() {
	sql := "select * from customer"
	stmt, _ := sqlparser.Parse(sql)
	switch stmt := stmt.(type) {
	case *sqlparser.Select:
		fmt.Printf("insert:%v", stmt)
	case *sqlparser.Insert:
	}

}

// func main() {
// 	etcd := distributed.GetEtcd()
// 	etcd.Write("/table/book", config.Book_table.Parse())
// 	etcd.Write("/table/customer", config.Customer_table.Parse())
// 	etcd.Write("/table/orders", config.Orders_table.Parse())
// 	etcd.Write("/table/publisher", config.Publisher_table.Parse())
// }

// func main() {
// 	etcd := distributed.GetEtcd()
// 	// book := etcd.Read("/table/Book")
// 	// customer := etcd.Read("/table/Customer")
// 	orders, _ := etcd.Read("/table/orders")
// 	publisher, _ := etcd.Read("/table/publisher")
// 	// fmt.Printf("book: %v\n", book)
// 	// fmt.Printf("customer: %v\n", customer)
// 	fmt.Printf("orders: %v\n", orders)
// 	fmt.Printf("publisher: %v\n", publisher)
// }

// func main() {
// 	sqlFile, err := utils.NewSqlFile("/Users/yu/Desktop/dataset/orders2.sql")
// 	if err != nil {
// 		fmt.Printf("err: %v\n", err)
// 	}
// 	for sqlFile.HasNext() {
// 		sql := sqlFile.Next()
// 		fmt.Printf("sql: %v\n", sql)
// 		insertSql(sql)
// 	}
// sql := "INSERT INTO publisher VALUES (100163,'Authors Publishing','GA');"
// insertSql(sql)
// sql2 := sqlFile.Next()
// sql := "INSERT INTO book VALUES (200002,'Standing in the Shadows','Richard Bruce Wright',101787,2900)"
// fmt.Printf("sql: %v\n", sql2)
// insertSql(sql2)
// sql := "INSERT INTO customer VALUES (305001,'CHAD CABELLO','F');"
// sql := "INSERT INTO Customer(id, name,gender) VALUES(310003,'zhangjie','man')"
// }

func insertSql(sql string) {
	// sql := "INSERT INTO Book(id,title,authors,publisher_id,copies) VALUES(4,'Death','Arthur',3,45);"
	// sql := "INSERT INTO Book(id,title,authors,publisher_id,copies) VALUES(3,'A book','Arthur',3,78);"
	// sql := "INSERT INTO Customer(id, name,gender) VALUES(310003,'zhangjie','man')"
	// sql := "INSERT INTO Customer(id, name,gender) VALUES(305001,'zhaoyue','woman')"
	// sql := "INSERT INTO Customer(id, name,gender) VALUES(310001,'luhaown','woman')"
	// sql := "select book.title, vbook.authors from book, publisher  where book.publisher_id=publisher.id and
	// publisher.name='Twomorrows Publishing' and id < 30500"
	stmt, err_sql := sqlparser.Parse(sql)
	if err_sql != nil {
		fmt.Printf("sql解析错误: %v\n", err_sql)
		return
	}
	stmtInsert, ok := stmt.(*sqlparser.Insert)
	if !ok {
		fmt.Printf("你输入的不是insert语句")
		return
	}
	table_name := stmtInsert.Table.Name
	etcd := distributed.GetEtcd()
	kv, err := etcd.ReadStruct("/table/" + table_name.String())
	if err != nil {
		fmt.Printf("etcd读取错误: %v\n", err)
		return
	}
	tableInfo, ok := kv.(*db.TableInfo)
	siteList, sqlList := tableInfo.InsertPrime(sql)
	if len(siteList) != len(sqlList) {
		fmt.Printf("生成sql异常\n")
		return
	}
	for i, v := range siteList {
		etcd.Write("/sql/insert/"+v, sqlList[i])
	}
}
